#import "PlayFab<%- api.name %>API.h"
#import "PlayFabConnection.h"

#import <UIKit/UIKit.h>
#include <sys/sysctl.h>

#import "JAGPropertyConverter.h"


@implementation PlayFab<%- api.name %>API

+(NSString*) GetURL {
return [NSString stringWithFormat:@"https://%@%@", [PlayFabSettings.VerticalName length] != 0 ? PlayFabSettings.VerticalName : PlayFabSettings.TitleId, PlayFabSettings.ProductionEnvironmentURL];
}

static PlayFabClientAPI* PlayFabInstance;

+ (PlayFabClientAPI*) GetInstance {
if(PlayFabInstance == NULL) {
PlayFabInstance = [PlayFabClientAPI new];
}
return PlayFabInstance;
}

+ (NSString *)getModel {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *model = malloc(size);
sysctlbyname("hw.machine", model, &size, NULL, 0);
NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
free(model);
return deviceModel;
}

<% if(api.name == "Client") { %>+(bool)IsClientLoggedIn {
    return !([[PlayFab<%- api.name -%>API GetInstance].mUserSessionTicket length]==0);
}

#ifdef USE_IDFA
-(void) MultiStepClientLogin:(bool) needsAttribution {
    if (needsAttribution && [PlayFabSettings.AdvertisingIdValue length] != 0) {
        [PlayFabSettings setAdvertisingIdValue:[PlayFabSettings identifierForAdvertising]];
    }
    // Send the ID when appropriate
    if (needsAttribution && [PlayFabSettings.AdvertisingIdValue length] != 0) {
        <%- api.name -%>AttributeInstallRequest* install_request = [<%- api.name -%>AttributeInstallRequest new];
        install_request.Idfa = PlayFabSettings.AdvertisingIdValue;

        [[PlayFabClientAPI GetInstance] AttributeInstall:install_request
        success:^(<%- api.name -%>AttributeInstallResult* result, NSObject* userData) {
            // Modify AdvertisingIdType:  Prevents us from sending the id multiple times, and allows automated tests to determine id was sent successfully
            [PlayFabSettings setAdvertisingIdType:[NSString stringWithFormat:@"%@%@",PlayFabSettings.AdvertisingIdType,@"_Successful"]];
            NSLog(@"playfab adid %@", PlayFabSettings.AdvertisingIdType);
        }
        failure:^(PlayFabError *error, NSObject *userData) {
            NSLog(@"attribute install fail");
            //Request errored or failed to connect.
        } withUserData:nil];
    }
}
#endif
<% }
for(var i in api.calls) { var apiCall = api.calls[i];
if(HasRequest(apiCall, api)) {
%>-(void) <%- apiCall.name %>:(<%- api.name %><%- apiCall.request %>*)request success:(<%- apiCall.name %>Callback)callback failure:(ErrorCallback)errorCallback withUserData:(NSObject*)userData
<% } else {
%>-(void) <%- apiCall.name %>:(<%- apiCall.name %>Callback)callback failure:(ErrorCallback)errorCallback withUserData:(NSObject*) userData
<% } 
%>{
    <%- GetRequestActions(apiCall, api) %>
    <% if(HasRequest(apiCall, api)) { %>
    NSString *jsonString = [request JSONStringWithClass:[<%- api.name %><%- apiCall.request %> class]];
    <% } else {%>
    NSString *jsonString = @"{}";
    <% } %>
    PlayFabConnection * connection = [PlayFabConnection new];//[[MyConnection alloc]initWithRequest:req];
    [connection setCompletionBlock:^(id obj, NSError *err) {
        NSData * data = obj;
        if (!err) {
            //NSLog(@"connection success response: %@",(NSString*)data);
            NSError *e = nil;
            NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error: &e];

            NSString* playfab_error = [JSON valueForKey:@"error"];
            if (playfab_error != nil) {
                //if there was an "error" object in the JSON:
                PlayFabError *playfab_error_object = [[PlayFabError new] initWithDictionary:JSON];
                errorCallback (playfab_error_object, userData);
            } else {
                NSDictionary *class_data = [JSON valueForKey:@"data"];
                <%- api.name %><%- apiCall.result%> *model = [[<%- api.name %><%- apiCall.result%> new] initWithDictionary:class_data];
                <%- GetResultActions(apiCall, api) %>
                callback (model, userData);
            }
        } else { //Connection Error:
            NSError *e = nil;
            NSLog(@"connection error response: %@",data);
            PlayFabError *model;
            if (data != nil) {
                NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &e];
                JAGPropertyConverter *converter = [JAGPropertyConverter new];
                model = [converter composeModelFromObject:JSON];
            } else {
                model = [PlayFabError new];
                model.error = @"unknown, data empty.";
            }
        errorCallback (model, userData);
        }
    }];

    [connection postURL:[NSString stringWithFormat:@"%@%@",[PlayFabClientAPI GetURL],@"<%- apiCall.url %>"] body:jsonString <%- GetAuthParams(apiCall) %>];
}
<% } %>
@end
